//
//  net.swift
//  WeiboClassSix
//
//  Created by HM on 16/10/7.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit

extension NetworkTool  {
    
    var oAuthURL: String {
        return "https://api.weibo.com/oauth2/authorize?client_id=\(appKey)&redirect_uri=\(redirectURI)"
    }
    
    /// 获取access_token
    ///
    /// - parameter code:     code
    /// - parameter callBack: 完成回调
    func requestAccessToken (code: String, callBack: @escaping (Any?)->()) {
        //调用网络中间层的request方法, 获取 acccess_token的数据, 获取完数据之后,回传给controller
        //使用code获取accessToken
        
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        let method = "POST"
        let parameters = ["client_id": appKey,
                          "client_secret": appSecrect,
                          "grant_type": "authorization_code",
                          "code": code,
                          "redirect_uri": redirectURI]
        
        NetworkTool.shared.requet(url: urlStr, method: method, paramerters: parameters, callBack: { (responseObject) in
            callBack(responseObject)
        })
    }
    
    
    /// 获取用户信息的网络业务
    ///
    /// - parameter uid:          uid用户id
    /// - parameter access_token: access_token
    /// - parameter callBack:     完成回调
    func reqeustUserInfo (uid: String, access_token: String, callBack: @escaping (Any?)->()) {
        let urlStr = "https://api.weibo.com/2/users/show.json"
        let method = "GET"
        let parameters = ["uid": uid,
                          "access_token": access_token]
        
        NetworkTool.shared.requet(url: urlStr, method: method, paramerters: parameters, callBack: { (responseObject) in
            callBack(responseObject)
        })
    }
    
    
}
